package com.service.Impl;

import com.dao.StorageMapper;
import com.entity.StorageRecord;
import com.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("storageService")
public class StorageServiceImpl implements StorageService {
    @Autowired
    private StorageMapper storageMapper;

    public void setStorageMapper(StorageMapper storageMapper) {
        this.storageMapper = storageMapper;
    }

    @Override
    public boolean add(StorageRecord storage) {

        boolean result = false;
        try{
            if (storageMapper.add(storage) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    public int delete(int id){
        int result = 0;
        try{
            if (storageMapper.delete(id) == 1){
                result = 1;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
